package itsense.client;

import java.util.ArrayList;

import itsense.component.ITSButton;
import itsense.model.MemberDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

import com.smartgwt.client.widgets.Label;  

import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuButton;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BeautySenseGWT implements EntryPoint {

	public static final BeautySenseServiceAsync service = GWT.create(BeautySenseService.class);
	public static  MemberListPanel memberListPanel = null;
	public static  ClientListPanel clientListPanel = null;
	public static MemberDTO member = null;
	private static LoginPanel loginPanel = null;
	private static Menu mainMenu = null;
	public static MenuItem chartMenuItem = null;
	public static MenuItem memberReportMenuItem = null;
	public static MenuItem memberMenuItem = null;
	public static MenuItem logoutMenuItem = null;
	public static MenuItem clientMenuItem = null;
	public static VLayout workspaceLayout = null;
	
	public void onModuleLoad() {
		showLoginPanel();
		//test();
	}
	private void test(){
		
		member = new MemberDTO();
		member.setFirstName("Przemyslaw");
		member.setLastName("Pretki");
		member.setEmail("p.pretki@issi.uz.zgroa.pl");
		member.setLogin("p.pretki");
		member.setPassword("ikterph");
		member.setId(new Long(0));
		ArrayList<Long> roles = new ArrayList<Long>();
		roles.add(new Long(0));
		roles.add(new Long(1));
		roles.add(new Long(2));
		member.setRoles(roles);
		buildUI();
		
	}
	public static void showLoginPanel(){
		for (int i = 0 ; i < RootPanel.get().getWidgetCount();i++){
			RootPanel.get().remove(i);
		}
		if (memberListPanel != null) memberListPanel.destroy();
		
		memberListPanel = null;
		member = null;
		loginPanel = null;
		mainMenu = null;
		chartMenuItem = null;
		memberReportMenuItem = null;
		memberMenuItem = null;
		logoutMenuItem = null;
		clientMenuItem = null;
		workspaceLayout = null;
		
		loginPanel = new LoginPanel(service);
		RootPanel.get().add(loginPanel);
	}
	
	public static void hideLoginPanel(){
		if (loginPanel != null){
			loginPanel.destroy();
			RootPanel.get().remove(loginPanel);
			loginPanel = null;
		}
	}

	
	
	
	private static HStack buildMenuUI(){

		HStack hStack = new HStack();
		hStack.setWidth100();
		hStack.setHeight(20);
		
		
		mainMenu = new Menu();
		
		mainMenu.setShowShadow(true);  
		mainMenu.setShadowDepth(10);
		
//		chartMenuItem = new MenuItem("Chart", "check-user-icon.png", "Ctrl+C");
		
		
		
		MenuButton adminMenuButton = new MenuButton("Admin",mainMenu);   
		MenuButton reportMenuButton = new MenuButton("Reports",mainMenu);
		MenuButton clientsMenuButton = new MenuButton("Clients",mainMenu);
		MenuButton employeeMenuButton = new MenuButton("Employee",mainMenu);
	
		
		for (Long role: member.getRoles()){
			switch(role.intValue()){
			case 0: //admin
				memberMenuItem = new MenuItem("Member", "check-user-icon.png", "Ctrl+M");
				memberMenuItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
					public void onClick(MenuItemClickEvent event) {
						BeautySenseGWT.memberListPanel = new MemberListPanel();
						workspaceLayout.addChild(BeautySenseGWT.memberListPanel);	
						BeautySenseGWT.memberMenuItem.setEnabled(false);
					
					}
				});
				
				clientMenuItem = new MenuItem("Client", "Log-Out-icon.png", "Ctrl+C");
				clientMenuItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
					public void onClick(MenuItemClickEvent event) {
						BeautySenseGWT.clientListPanel = new ClientListPanel();
						workspaceLayout.addChild(BeautySenseGWT.clientListPanel);	
						BeautySenseGWT.clientMenuItem.setEnabled(false);
					
					}
				});
				
				mainMenu.addItem(clientMenuItem);
				mainMenu.addItem(memberMenuItem);
				
				break;
			};
		};
		logoutMenuItem = new MenuItem("Logout", "Log-Out-icon.png", "Ctrl+X");
		logoutMenuItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
			public void onClick(MenuItemClickEvent event) {
				showLoginPanel();
			}
		});
		
		mainMenu.addItem(logoutMenuItem);
		
		hStack.addMember(adminMenuButton);
		hStack.addMember(reportMenuButton);
		hStack.addMember(clientsMenuButton);
		hStack.addMember(employeeMenuButton);
		
		return hStack;
	} 
	
	private static ToolStrip buildToolBarUI(){
		ToolStrip toolStrip = new ToolStrip();  
		toolStrip.setWidth100();
		toolStrip.setHeight(50);
		
		IButton clientsButton = new IButton();
		clientsButton.setWidth(48);
		clientsButton.setHeight(48);
		clientsButton.setIconSize(32);
		Label label = new Label();  
		label.setContents("Globe");  
		label.setAlign(Alignment.CENTER);  
		label.setTop(20);
		label.setLeft(-5);
		clientsButton.addChild(label);
		clientsButton.setIcon("globe-icon.png");
		toolStrip.addMember(clientsButton);
		
		ITSButton button = new ITSButton("globe-icon.png", "Clients");
		toolStrip.addMember(button);
		
		
		return toolStrip;
	}
	
	private static  SectionStack buildStackSectionsUI(){
		SectionStack sectionStack = new SectionStack();  
		sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);  
		sectionStack.setWidth(150);  
		sectionStack.setHeight100();  
		SectionStackSection section1 = new SectionStackSection("Client options");  
		section1.setExpanded(true);  
		sectionStack.addSection(section1);
		
		SectionStackSection section2 = new SectionStackSection("General options");  
		section1.setExpanded(true);  
		sectionStack.addSection(section2);

		SectionStackSection section3 = new SectionStackSection("Search");  
		section1.setExpanded(true);  
		sectionStack.addSection(section3);
		
		sectionStack.setShowResizeBar(true);
		return sectionStack;
	}
	
	
	public static void buildUI(){

		VLayout vLayout = new VLayout();
		vLayout.setHeight100();
		vLayout.setWidth100();
		vLayout.addMember(buildMenuUI());
		vLayout.addMember(buildToolBarUI());

		HLayout hLayout = new HLayout();
		hLayout.setWidth100();  
		hLayout.setHeight100();
		hLayout.addMember(buildStackSectionsUI());
		
		workspaceLayout = new VLayout();
		
		
		hLayout.addMember(workspaceLayout);
		vLayout.addMember(hLayout);
		
		vLayout.draw();
		
		
	}
	
}
